package com.harisucici.websocketlib.utils;

import com.google.gson.Gson;
import com.harisucici.websocketlib.entities.receive.CurrentSessionReceive;
import com.harisucici.websocketlib.entities.receive.MyFriends;
import com.harisucici.websocketlib.entities.receive.SessionListReceive;
import com.harisucici.websocketlib.entities.send.CurrentSessionSend;
import com.harisucici.websocketlib.entities.send.P2PSend;
import com.harisucici.websocketlib.entities.send.P2PSendHoleEntity;
import com.harisucici.websocketlib.entities.send.SendMsgEntity;
import com.harisucici.websocketlib.im.HWebSocketManager;
import com.harisucici.websocketlib.modle.BaseSendEntity;

public class Commands {

    private static Commands commands;
    private static Gson gson;

    public static synchronized Commands getInstance() {
        if (commands == null) {
            commands = new Commands();
            gson = new Gson();
        }
        return commands;
    }

    /**
     * 获取最近联系人列表
     * 建议getTalkingListOK
     */
    @Deprecated
    public void getTalkingList(String account) {
        BaseSendEntity entity = Utils.getInstance().buildSend(Constants.SESSION_LIST, account, account);
        String content = gson.toJson(entity);
        HWebSocketManager.getInstance().getService().send(content);
    }

    public String makeTalkingList(String account) {
        Builder builder = new BaseEntityBuild();
        builder.setType(Constants.SESSION_LIST);
        builder.setFromAccount(account);
        builder.setToAccount(account);
        return builder.createContent();
    }

//    public void getTalkingListOK(String account) {
//        BaseSendEntity entity = Utils.getInstance().buildSend(Constants.SESSION_LIST, account, account);
//        String content = gson.toJson(entity);
//        HWebSocketOKManager.getInstance().send(content);
//    }


    /**
     * 获取会话列表
     * 建议getTalkingDetailOk
     */
    @Deprecated
    public void getTalkingDetail(String account, String timelineId, long sequenceId) {
        BaseSendEntity entity = Utils.getInstance().buildSend(Constants.CURRENT_SESSION, account, account);
        CurrentSessionSend currentSessionSend = new CurrentSessionSend();
        currentSessionSend.setTimelineId(timelineId);
        currentSessionSend.setSequenceId(sequenceId);
        entity.setPayLoad(currentSessionSend);
        String content = gson.toJson(entity);
        HWebSocketManager.getInstance().getService().send(content);
    }

    public String makeTalkingDetail(String account, String timelineId, long sequenceId) {
        Builder builder = new BaseEntityBuild();
        builder.setType(Constants.CURRENT_SESSION);
        builder.setFromAccount(account);
        builder.setToAccount(account);
        CurrentSessionSend currentSessionSend = new CurrentSessionSend();
        currentSessionSend.setTimelineId(timelineId);
        currentSessionSend.setSequenceId(sequenceId);
        builder.setPayLoad(currentSessionSend);
        return builder.createContent();
    }


    /**
     * 生产聊天消息
     * @return
     */
    public P2PSendHoleEntity makeMsg(SendMsgEntity sendMsgEntity) {
        P2PSend p2pSend = new P2PSend();
        p2pSend.setAppSeqId(Utils.getInstance().makeAppSeqId());
        p2pSend.setTimestamp(System.currentTimeMillis());
        setP2PContent(p2pSend,sendMsgEntity.getMsgType(),sendMsgEntity.getMsg(),sendMsgEntity.getMsgTime());
//        p2pSend.setMsgType(sendMsgEntity.getMsgType());
//        p2pSend.setContent(sendMsgEntity.getMsg());
        // timelineId
        String toAccount = null;
        if (sendMsgEntity.getCmdType().equals(Constants.P2P)) {
            if (sendMsgEntity.getEntity() instanceof SessionListReceive) {
                p2pSend.setTimelineId(((SessionListReceive) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((SessionListReceive) sendMsgEntity.getEntity()).getUserId();
            } else if (sendMsgEntity.getEntity() instanceof MyFriends) {
                p2pSend.setTimelineId(((MyFriends) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((MyFriends) sendMsgEntity.getEntity()).getUserId();
            }
        } else if (sendMsgEntity.getCmdType().equals(Constants.GROUP)) {
            if (sendMsgEntity.getEntity() instanceof SessionListReceive) {
                p2pSend.setTimelineId(((SessionListReceive) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((SessionListReceive) sendMsgEntity.getEntity()).getTimelineId();
            } else if (sendMsgEntity.getEntity() instanceof MyFriends) {
                p2pSend.setTimelineId(((MyFriends) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((MyFriends) sendMsgEntity.getEntity()).getTimelineId();
            }
        }
        P2PSendHoleEntity entity = Utils.getInstance().buildSendP2P(sendMsgEntity.getCmdType(), sendMsgEntity.getFromAccount(), toAccount);
        entity.setPayLoad(p2pSend);
        // p2p 转换 cs,设置为未发送
        CurrentSessionReceive currentEntity = Utils.getInstance().p2c(entity, sendMsgEntity.getEntity());
        currentEntity.setTime(sendMsgEntity.getMsgTime());
        currentEntity.setSendState(Constants.UN_SEND);
        //存CS
        sendMsgEntity.getcDao().insertMsg(currentEntity);
        // 存PS
        sendMsgEntity.getpDao().insertMsg(entity);
        // p2p 转换 sl,设置为未发送  存Sl
        saveSl(entity, sendMsgEntity);
        //发送
//        String content = gson.toJson(entity);
        return entity;
//        HWebSocketManager.getInstance().getService().send(content);
    }

   /* public String makeMsgOK(SendMsgEntity sendMsgEntity) {
        P2PSend p2pSend = new P2PSend();
        p2pSend.setAppSeqId(Utils.getInstance().makeAppSeqId());
        p2pSend.setTimestamp(System.currentTimeMillis());
        setP2PContent(p2pSend,sendMsgEntity.getMsgType(),sendMsgEntity.getMsg(),sendMsgEntity.getMsgTime());
//        p2pSend.setContent(sendMsgEntity.getMsg());
//        p2pSend.setMsgType(sendMsgEntity.getMsgType());
        // timelineId
        String toAccount = null;
        if (sendMsgEntity.getCmdType().equals(Constants.P2P)) {
            if (sendMsgEntity.getEntity() instanceof SessionListReceive) {
                p2pSend.setTimelineId(((SessionListReceive) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((SessionListReceive) sendMsgEntity.getEntity()).getUserId();
            } else if (sendMsgEntity.getEntity() instanceof MyFriends) {
                p2pSend.setTimelineId(((MyFriends) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((MyFriends) sendMsgEntity.getEntity()).getUserId();
            }
        } else if (sendMsgEntity.getCmdType().equals(Constants.GROUP)) {
            if (sendMsgEntity.getEntity() instanceof SessionListReceive) {
                p2pSend.setTimelineId(((SessionListReceive) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((SessionListReceive) sendMsgEntity.getEntity()).getTimelineId();
            } else if (sendMsgEntity.getEntity() instanceof MyFriends) {
                p2pSend.setTimelineId(((MyFriends) sendMsgEntity.getEntity()).getTimelineId());
                toAccount = ((MyFriends) sendMsgEntity.getEntity()).getTimelineId();
            }
        }
        P2PSendHoleEntity entity = Utils.getInstance().buildSendP2P(sendMsgEntity.getCmdType(), sendMsgEntity.getFromAccount(), toAccount);
        entity.setPayLoad(p2pSend);
        // p2p 转换 cs,设置为未发送
        CurrentSessionReceive currentEntity = Utils.getInstance().p2c(entity, sendMsgEntity.getEntity());
        currentEntity.setTime(sendMsgEntity.getMsgTime());
        currentEntity.setSendState(Constants.UN_SEND);
        //存CS
        sendMsgEntity.getcDao().insertMsg(currentEntity);
        // 存PS
        sendMsgEntity.getpDao().insertMsg(entity);
        // p2p 转换 sl,设置为未发送  存Sl
        saveSl(entity, sendMsgEntity);
        //发送
        String content = gson.toJson(entity);
        return content;
//        HWebSocketOKManager.getInstance().send(content);
    }*/

    private void saveSl(P2PSendHoleEntity p2PSendHoleEntity, SendMsgEntity sendMsgEntity) {
        SessionListReceive sl = new SessionListReceive();
        if (sendMsgEntity.getEntity() instanceof MyFriends) {
            MyFriends mf = (MyFriends) sendMsgEntity.getEntity();
            // 最近会话进入
            sl.setUserNo(mf.getUserNo());

//            sl.setUnReadNum(mf.);
            sl.setTextAvator(mf.getTextAvator());
//            sl.setCreateTime(mf.);
            sl.setName(mf.getUserName());
            sl.setUserId(mf.getUserId());
            sl.setTimelineId(mf.getTimelineId());
//            sl.setTop(mf.);
            sl.setType(p2PSendHoleEntity.getCmdType());
            sl.setImgAvator(mf.getImgAvator());
//            sl.setRemove(mf.);

            setSLContent(sl,p2PSendHoleEntity.getPayLoad());
//            sl.setMsgType(p2PSendHoleEntity.getPayLoad().getMsgType());
//            sl.setLastMessage(p2PSendHoleEntity.getPayLoad().getContent());



            sl.setTimestamp(p2PSendHoleEntity.getPayLoad().getTimestamp());
        } else if (sendMsgEntity.getEntity() instanceof SessionListReceive) {
            sl = (SessionListReceive) sendMsgEntity.getEntity();
            // 最近会话进入

            setSLContent(sl,p2PSendHoleEntity.getPayLoad());
//            sl.setLastMessage(p2PSendHoleEntity.getPayLoad().getContent());
            sl.setTimestamp(p2PSendHoleEntity.getPayLoad().getTimestamp());
        }
        sl.setSendState(Constants.UN_SEND);
        if (sendMsgEntity.getsDao().findSessionById(sl.getTimelineId()) == null) {
            sendMsgEntity.getsDao().insertSession(sl);
        } else {
            sendMsgEntity.getsDao().updateSession(sl);
        }

    }

    public void setSLContent(SessionListReceive sl ,P2PSend ps){
        sl.setMsgType(ps.getMsgType());
        if (ps.getMsgType().equals(Constants.TEXT)) {
            sl.setLastMessage(ps.getContent());
        } else if (ps.getMsgType().equals(Constants.IMAGE)) {
            sl.setLastMessage(ps.getImage());
        } else if (ps.getMsgType().equals(Constants.VIDEO)) {
            sl.setLastMessage(ps.getVideo());
        } else if (ps.getMsgType().equals(Constants.SYSTEM)) {
            sl.setLastMessage(ps.getSystem());
        } else if (ps.getMsgType().equals(Constants.AUDIO)) {
            sl.setLastMessage(ps.getAudio());
        } else if (ps.getMsgType().equals(Constants.LINK)) {
            sl.setLastMessage(ps.getLink());
        } else {
            sl.setLastMessage(ps.getNormal());
        }
    }
    // 发送消息内容物设置
    public void setP2PContent(P2PSend p2pSend, String type, String content,Long time) {

        p2pSend.setMsgType(type);


//        public final static String NORMAL = "NORMAL";
//        public final static String TEXT = "TEXT";
//        public final static String IMAGE = "IMAGE";
//        public final static String VIDEO = "VIDEO";
//        public final static String SYSTEM = "SYSTEM";
//        public final static String AUDIO = "AUDIO";
//        public final static String LINK = "LINK";

        if (type.equals(Constants.TEXT)) {
            p2pSend.setContent(content);
        } else if (type.equals(Constants.IMAGE)) {
            p2pSend.setImage(content);
        } else if (type.equals(Constants.VIDEO)) {
            p2pSend.setVideo(content);
            p2pSend.setTime(time);
        } else if (type.equals(Constants.SYSTEM)) {
            p2pSend.setSystem(content);
        } else if (type.equals(Constants.AUDIO)) {
            p2pSend.setAudio(content);
            p2pSend.setTime(time);
        } else if (type.equals(Constants.LINK)) {
            p2pSend.setLink(content);
        } else {
            p2pSend.setNormal(content);
        }


    }


}
